package com.starpl.mobile.android.monthunsae.november.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.starpl.mobile.android.monthunsae.november.R;
import com.starpl.mobile.android.monthunsae.november.domain.UserInfo;
import com.starpl.mobile.android.monthunsae.november.handler.ProfileListHandler;
import com.starpl.mobile.android.platform.command.StarplCommand;
import com.starpl.mobile.android.platform.common.base.BaseActivity;
import com.starpl.mobile.android.platform.common.base.BaseApplication;
import com.starpl.mobile.android.platform.common.image.ImageDownloader;
import com.starpl.mobile.android.platform.common.network.HttpHandler;
import com.starpl.mobile.android.platform.component.StarplListView;

/**
 * 유저(나, 다른유저) 프로필을 보여주는 Activity
 * 
 * @author 우연화
 * @since 2011.02.22
 */
public class ProfileActivity extends BaseActivity implements OnClickListener
{
	private TextView mTvTitle;
	private ImageButton mIbSend;
	private ImageButton mIbInfoChange;
	private LinearLayout mLlForListView;
	
	private ProfileListHandler mListHandler;
	
	private String mUserId;
	private String mNickname;
	private int mUserIdx;
	
	/* (non-Javadoc)
	 * @see com.starpl.platform.common.base.BaseActivity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.profile_activity);
		
		mTvTitle = (TextView)findViewById(R.id.profile_activity_tv_title);
		mIbSend = (ImageButton)findViewById(R.id.profile_activity_ib_send_message);
		mIbInfoChange = (ImageButton)findViewById(R.id.profile_activity_ib_info_change);
		
		mLlForListView = (LinearLayout)findViewById(R.id.profile_activity_ll_for_listview);
		
		mIbSend.setOnClickListener(this);
		mIbInfoChange.setOnClickListener(this);
		
		// 이전 액티비티에서 넘겨준 유저정보 받아오기
		Bundle bundle = getIntent().getExtras();
		mUserId = bundle.getString("userId");
		mNickname = bundle.getString("nickname");
		mUserIdx = bundle.getInt("userIdx");
		
		mListHandler = new ProfileListHandler(this, mUserId);
		StarplListView lvPost = new StarplListView(mListHandler);
		mLlForListView.addView(lvPost);
		
		setSendButton(mUserId);
		mTvTitle.setText(mNickname);
	}
	
	@Override
	protected void onRestart()
	{
		super.onRestart();
		mListHandler.refresh();
	}

	/**
	 * 내 프로필인지, 다른 유저 프로필인지에 따라 쪽지보내기 버튼의 뷰 여부를 결정합니다.
	 * 
	 * @param profileUserId 프로필 유저 아이디
	 */
	private void setSendButton(String profileUserId)
	{
		UserInfo userInfo = (UserInfo)BaseApplication.getGlobalPersistent("userInfo");
		String userId = userInfo.getUserId();
		
		if(userId.equals(profileUserId))
		{
			mIbSend.setVisibility(ImageButton.GONE);
			mIbInfoChange.setVisibility(ImageButton.VISIBLE);
		}
		else
		{
			mIbSend.setVisibility(ImageButton.VISIBLE);
			mIbInfoChange.setVisibility(ImageButton.GONE);
		}
	}

	/* (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v)
	{
		switch(v.getId())
		{
		// 쪽지 보내기 버튼 클릭
		case R.id.profile_activity_ib_send_message:
			UserInfo userInfo = (UserInfo)BaseApplication.getGlobalPersistent("userInfo");
			
			Bundle extras = new Bundle();
			
			extras.putInt("sendUserIdx", mUserIdx);
			extras.putString("sendUserName", mNickname);
			extras.putString("sendUserId", mUserId);
			
			extras.putString("userId", userInfo.getUserId());
			extras.putString("nickname", userInfo.getNickName());
			
			getNewActivity()
				.setTargetActivity(MessageHistoryActivity.class)
				.setBundle(extras)
				.show();
			break;
		// 정보수정 버튼 클릭
		case R.id.profile_activity_ib_info_change:
			extras = new Bundle();
			extras.putBoolean("isChange", true);
			
			getNewActivity()
				.setTargetActivity(JoinExtraActivity.class)
				.setBundle(extras)
				.setRequestCode(17)
				.show();
			break;
		}
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		HttpHandler httpHandler = new HttpHandler(this)
		{
			@Override
			public void didSucceed(Object result)
			{
				// 유저 프로필 정보를 받아온다.
				try
				{
					JSONObject object = (JSONObject) result;
					JSONObject data = object.getJSONObject("data");
					String profileImgPath = data.getString("profileImgPath");
					ImageView profimeImgView = mListHandler.getProfileImage();
					
					// 프로필 헤더에 있는 사용자 이미지를 변경한다.
					if(profileImgPath != null && profimeImgView != null)
					{
						ImageDownloader.download(profileImgPath, profimeImgView, true);
					}
				}
				catch(JSONException e)
				{
					LOG.e(e.getMessage());
				}
			}
		};
		
		HttpHandler headerHandler = new HttpHandler(this)
		{
			@Override
			public void didSucceed(Object result)
			{
				// 유저 프로필 정보를 받아온다.
				mListHandler.setHeaderView(result);
			}
		};
		
		switch(requestCode)
		{
		// 프로필 이미지 변경	
		case 0 :
			StarplCommand.getUserInfo(httpHandler, mUserId);
			break;
		// 사용자 정보 수정
		case 17 :
			StarplCommand.getUserInfo(headerHandler, mUserId);
			break;
		}
	}
}
